package com.hotels.management;



import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import org.springframework.orm.hibernate3.LocalSessionFactoryBean;

import com.hotels.management.domain.Furniture;
import com.hotels.management.domain.FurnitureOrder;
import com.hotels.management.domain.Client;
import com.hotels.management.domain.FurnitureType;
import com.hotels.management.domain.Order;
import com.hotels.management.domain.Room;
import com.hotels.management.domain.RoomClass;



import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Just a test
 * 
 * @author Petr Udalau
 */
public class HibernateTest {
    private static ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[] {
                "applicationContext-hibernate.xml"//it is path to context from build folder
            });

    /**
     * DOCUMENT ME!
     *
     * @param args
     *
     * @throws IOException
     * @throws ClassNotFoundException
     */
    public static void main(String[] args) {
        LocalSessionFactoryBean localSessionFactoryBean = (LocalSessionFactoryBean) ctx
                .getBean("&sessionFactory");
        SessionFactory sessionFactory = (SessionFactory) localSessionFactoryBean.getObject();

        Session session = sessionFactory.openSession();
        Transaction tx = session.beginTransaction();

        Order receipt = new Order();

        Client client = new Client();
        client.setAddress("in ebeniah");
        client.setBithDate(new Date(88, 4, 22));
        client.setComingPurpose("tourism");
        client.setName("Petr");
        client.setSurname("Udalau");
        client.setPatronymic("Uladzimiravich");
        client.setPassport("MP1817284");
        
        RoomClass roomClass = new RoomClass();
        roomClass.setName("Lux");
        
        Room room = new Room();
        room.setRoomClass(roomClass);
        
        
        List<FurnitureOrder> addinsList = new ArrayList<FurnitureOrder>();
        
        Furniture tvSet = new Furniture();
        FurnitureType tvType = new FurnitureType();
        tvType.setName("tv");
        tvSet.setType(tvType);
        tvSet.setName("Horizont 1245");
        
        FurnitureOrder tvReceipt = new FurnitureOrder();
        tvReceipt.setFurniture(tvSet);
        tvReceipt.setCost(570.0);
        tvReceipt.setFromDate(new Date(150, 10, 5, 11, 0 ,0));
        tvReceipt.setToDate(new Date(150, 10, 6, 11, 0 ,0));
        tvReceipt.setOrder(receipt);
        
        Furniture refrigerator = new Furniture();
        FurnitureType refrigeratorType = new FurnitureType();
        refrigeratorType.setName("refrigerator");
        refrigerator.setType(refrigeratorType);
        refrigerator.setName("Atlant 112231");
        
        FurnitureOrder refrigeratorReceipt = new FurnitureOrder();
        refrigeratorReceipt.setFurniture(refrigerator);
        refrigeratorReceipt.setCost(990.0);
        refrigeratorReceipt.setFromDate(new Date(150, 10, 5, 11, 0 ,0));
        refrigeratorReceipt.setToDate(new Date(150, 10, 6, 11, 0 ,0));
        refrigeratorReceipt.setOrder(receipt);

        
        List<Furniture> furniture = new ArrayList<Furniture>();
        furniture.add(tvSet);
        furniture.add(refrigerator);
        room.setRoomFurniture(furniture);
        
        addinsList.add(tvReceipt);
        addinsList.add(refrigeratorReceipt);
        
        receipt.setClient(client);
        receipt.setRoomCost(21207.0);
        receipt.setLevy(1607.0);
        receipt.setTax(32136.0);
        receipt.setVAT(5356.0);
        receipt.setOverallCost(33740.0);
        receipt.setCheckinDate(new Date(150, 10, 5, 11, 0 ,0));
        receipt.setCheckoutDate(new Date(150, 10, 6, 11, 0 ,0));
        receipt.setRoom(room);
        receipt.setFurnitureOrders(addinsList);
        
        session.save(room);
        session.save(roomClass);
        session.save(tvType);
        session.save(tvSet);
        session.save(tvReceipt);
        session.save(refrigeratorType);
        session.save(refrigerator);
        session.save(refrigeratorReceipt);
        session.save(client);
        session.save(receipt);
        tx.commit();
        session.close();
    }
}